from __future__ import with_statement
from unittest import TestCase

from formencode import Invalid

from pdw.util import bag

from clutch import C
from clutch.wsgi import make_app_no_middleware
from clutch.topes import model
import debug

class TestTopeModels(TestCase):

    def setUp(self):
        self.app = make_app_no_middleware({}, **{
            'use':'egg:Clutch',
            'clutch.app':'TestApplication#main'})
        self.context = self.app.context()
        self.context.__enter__()

    def tearDown(self):
        self.context.__exit__(None, None, None)

    def testString(self):
        st = model.StringTope('foo', min_length=4, max_length=9)
        self.assert_(st.sa_column_type.length==9)
        self.assertRaises(Invalid, st.to_python, 'as')
        self.assertRaises(Invalid, st.to_python, '0123456789')
        st.to_python('asdf')

    def testSSN(self):
        st = model.SSNTope('ssn')
        self.assert_(st.sa_column_type.length==11)
        self.assertRaises(Invalid, st.to_python, '111-11-11111')
        self.assertRaises(Invalid, st.to_python, '111-11-111')
        st.to_python('111-11-1111')

    def testGroup(self):
        t = model.TopeGroup(
            'id',
            model.StringTope('name'),
            model.StringTope('address'),
            model.SSNTope('ssn'))
        t.sa_columns(self.app.model.AppColumn)
        t.to_python({})
        self.assertRaises(Invalid, t.to_python, dict(ssn='111'))

class OldTestTopes(TestCase):

    def setUp(self):
        self.app = make_app_no_middleware({}, **{
            'use':'egg:Clutch',
            'clutch.app':'TestApplication#main'})
        self.context = self.app.context()
        self.context.__enter__()

    def tearDown(self):
        self.context.__exit__(None, None, None)

    def testSimple(self):
        tf = TextField('foo', attrs=dict(dojoType='simpleForm'))
        txt = tf.render('asdf')
        self.assert_('name="foo" id="foo" value="asdf"' in txt)

    def testForm(self):
        sf = SimpleForm('myform', fields=[
            TextField('foo'),
            TextField('bar', label='Place to drink', help='My kind of place'),
            SSN('ssn', label='SSN'),
##            LongText('comments'),
##            ResizingLongText('more_comments'),
##            RichText('some_rich_text')
            ])
        txt = sf.render(bag(foo=5))
        print txt
        self.assert_('value="5"' in txt)
        self.assert_('Place to drink' in txt)
        self.assert_('My kind of place' in txt)
        
    def testNestedForm(self):
        sf = SimpleForm('myform', fields=[
            FieldSet('part1', fields=[TextField('foo'), TextField('bar')]),
            FieldSet('part2', fields=[TextField('foo'), TextField('bar')])
            ])
        txt = sf.render(bag(part1=bag(foo=5)))
        self.assert_('part1.foo"' in txt, txt)
        self.assert_('value="5"' in txt, txt)

    def testSimpleValidation(self):
        fld = RegexField(name='foo', regex='abc')
        self.assertRaises(Invalid, fld.validator.to_python, 'ddd')
        fld = SSN(name='ssn')
        try:
            fld.validator.to_python('ddd')
        except Invalid, err:
            self.assertEqual(err.message, 'Please enter in the format NNN-NN-NNNN')

    def testComplexValidation(self):
        sf = SimpleForm('a', fields=[
            TextField('name'),
            SSN('ssn')])
        value = bag(name='Rick', ssn='asdf')
        try:
            sf.validator.to_python(value)
        except Invalid, err:
            C.validation_errors = err.unpack_errors()
        txt = sf.render(value)
        self.assert_('Please enter in the format' in txt)
